
Partial Class SAD_SQWaitStatesL
    Inherits System.Web.UI.Page

    Private Const Q As String = Chr(34)

    Private moServer As SQLAutoDocLib.Server = Nothing
    Private moDatabase As SQLAutoDocLib.Database = Nothing

    Private msDBID As String
    Private msWaitState As String

    Private msStartDate As String
    Private msEndDate As String
    Private mdIntervals As Decimal = 0

    Private mbShowDetail As Boolean = False

    Private moChart As jChart.GraphChart
    Private moTrendData As DataTable = Nothing

    Private Const NODATA As Long = -999

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = True Then
            msDBID = hidDBID.Value
            msWaitState = hidTBID.Value

            msStartDate = hidSD.Value
            msEndDate = hidED.Value

            If hidToolbarAction.Value = "view" Then
                Dim sURL As String = "ShowViewWindow('" & _
                                             "SQWaitStatesL.aspx?D=" & msDBID & _
                                             "&SD=" & HttpUtility.UrlEncode(msStartDate) & _
                                             "&ED=" & HttpUtility.UrlEncode(msEndDate) & _
                                             "&T=" & msWaitState & _
                                             "');"

                RegisterClientScript("ViewWindow", sURL)
            End If
        Else
            msDBID = Request.QueryString("D")
            hidDBID.Value = msDBID

            msWaitState = Request.QueryString("T")
            hidTBID.Value = msWaitState
        End If

        moDatabase = New SQLAutoDocLib.Database(msDBID)
        moDatabase.Load()

        moServer = New SQLAutoDocLib.Server(moDatabase.ServerID)
        moServer.Load()

        Dim oFactory As New SQLAutoDocLib.WaitStateLog_Factory
        divWSDesc.InnerHtml = "<pre>" & oFactory.GetDescriptionForState(msWaitState) & "</pre>"

        If IsDate(Request.QueryString("SD")) Then
            msStartDate = Request.QueryString("SD")
            hidSD.Value = msStartDate

            msEndDate = Request.QueryString("ED")
            hidED.Value = msEndDate

            If Not IsDate(msEndDate) Then msEndDate = Now

            mbShowDetail = True
            mdIntervals = DateDiff(DateInterval.Hour, CDate(msStartDate), CDate(msEndDate))

            tdCaption.InnerText = "WaitState Trends for " & msWaitState & " by hour"
            AZoom.HRef = "SQWaitStatesL.aspx?D=" & moDatabase.ID & "&T=" & msWaitState
            ARefresh.HRef = "SQWaitStatesL.aspx?D=" & moDatabase.ID & _
                                             "&SD=" & HttpUtility.UrlEncode(msStartDate) & _
                                             "&ED=" & HttpUtility.UrlEncode(msEndDate) & _
                                             "&T=" & msWaitState

            moTrendData = oFactory.WaitStateDetail(moDatabase.ID, msStartDate, msEndDate, msWaitState)
            MakeWaitStateDetailChart()

        Else
            msStartDate = DateAdd(DateInterval.Weekday, -23, Now)
            msEndDate = Now
            mdIntervals = DateDiff(DateInterval.Day, CDate(msStartDate), CDate(msEndDate)) / 10

            tdCaption.InnerText = "WaitState Trends for " & msWaitState & " by day"
            AZoom.HRef = "SQWaitStatesE.aspx?S=" & moDatabase.ServerID & "&D=" & moDatabase.ID
            ARefresh.HRef = "SQWaitStatesL.aspx?D=" & moDatabase.ID & "&T=" & msWaitState

            moTrendData = oFactory.WaitState10PointTrends(moDatabase.ID, msStartDate, msEndDate, msWaitState)
            MakeWaitStateWeeklyChart()
        End If
    End Sub

    Protected Function FormLabel() As String
        Dim oFactory As New SQLAutoDocLib.ScanHistory_Factory
        Dim sCurrentScan As String = ""
        Dim sLastScan As String = ""
        oFactory.GetTimeOfLastScan(moDatabase.ID, SQLAutoDocLib.ScanSchedule.Type.WaitStateScan, "SQLAutoDocLib.WaitStateLog", sCurrentScan, sLastScan)

        Dim sValue As String = ""

        If mbShowDetail Then
            sValue = "Wait State Trends by hour for " & moDatabase.Name & " from " & msStartDate & " to " & msEndDate & " last scan completed " & sLastScan
        Else
            sValue = "Wait State Trends by day for " & moDatabase.Name & " from " & msStartDate & " to " & msEndDate & " last scan completed " & sLastScan
        End If

        If sCurrentScan > "" Then
            sValue = " scan in progress started " & sCurrentScan
        End If

        Return sValue
    End Function

    Protected Function ScanMode() As String
        Return "Size"
    End Function

    Protected Function EditMode() As String
        Return "Size"
    End Function

    Protected Function ServerID() As String
        Return moServer.ID
    End Function

    Private Sub MakeWaitStateWeeklyChart()
        '---Let's set up the chart.
        '   We'll make it 600 pixels wide by 400 pixels high.        
        moChart = New jChart.GraphChart(510, 260)
        moChart.ChartName = "Average WaitStates - " & msWaitState
        moChart.ChartDimensions.BackgroundColor = Color.White

        '---We'll specify a 10 pixel border all the way around the inside
        '   edge of the chart.
        moChart.ChartDimensions.FrameSize = 10

        '---The Y Axis column on the left will be 20 pixels wide.
        moChart.ChartDimensions.YAxis1LabelColumnWidth = 20

        '---The X Axis row on the bottom will be 20 pixels high.
        moChart.ChartDimensions.XAxisLabelRowHeight = 20

        '---Let's add the X Axis values
        '   We'll have 7 values and in place of the numeric values we'll
        '   display friendly labels.
        Dim lCount As Integer = 10
        Dim lMax As Long = 0

        Dim oXAxisValues(9) As Single
        Dim oXAxisLabels(9) As String
        Dim oAvgRequestValues(lCount) As Single
        Dim oAvgWaitTimeValues(lCount) As Single
        Dim oAvgSignalTimeValues(lCount) As Single

        Dim dSD_Count_1 As Integer = 0
        Dim sSDSumSQR_1 As Single = 0
        Dim sSDSum_1 As Single = 0
        Dim sSDAverage_1 As Single = 0
        Dim sSDInterim_1 As Single = 0
        Dim sSDMin_1 As Single = Single.NaN
        Dim sSDMax_1 As Single = Single.NaN
        Dim sSDStdDev_1 As Single

        Dim dSD_Count_2 As Integer = 0
        Dim sSDSumSQR_2 As Single = 0
        Dim sSDSum_2 As Single = 0
        Dim sSDAverage_2 As Single = 0
        Dim sSDInterim_2 As Single = 0
        Dim sSDMin_2 As Single = Single.NaN
        Dim sSDMax_2 As Single = Single.NaN
        Dim sSDStdDev_2 As Single

        Dim dSD_Count_3 As Integer = 0
        Dim sSDSumSQR_3 As Single = 0
        Dim sSDSum_3 As Single = 0
        Dim sSDAverage_3 As Single = 0
        Dim sSDInterim_3 As Single = 0
        Dim sSDMin_3 As Single = Single.NaN
        Dim sSDMax_3 As Single = Single.NaN
        Dim sSDStdDev_3 As Single

        '---Calculate standard deviation
        '---Calculate count and sum of data points
        '	We'll also now calculate min, max and average.
        For i As Integer = 1 To 10
            oXAxisValues(i - 1) = i - 1
            oXAxisLabels(i - 1) = DateAdd(DateInterval.Day, (i - 1) * mdIntervals, CDate(msStartDate)).Date.ToString("MM/dd/yyyy")

            If IsNumeric(moTrendData.Rows(0).Item("P" & i & "_AvgRequests")) Then
                Dim dValue As Single = moTrendData.Rows(0).Item("P" & i & "_AvgRequests")
                oAvgRequestValues(i - 1) = dValue

                If Single.IsNaN(sSDMin_1) Then
                    sSDMin_1 = dValue

                ElseIf dValue < sSDMin_1 Then
                    sSDMin_1 = dValue
                End If

                If Single.IsNaN(sSDMax_1) Then
                    sSDMax_1 = dValue

                ElseIf dValue > sSDMax_1 Then
                    sSDMax_1 = dValue
                End If

                dSD_Count_1 = dSD_Count_1 + 1
                sSDSumSQR_1 = sSDSumSQR_1 + dValue * dValue
                sSDSum_1 = sSDSum_1 + dValue
            End If

            If IsNumeric(moTrendData.Rows(0).Item("P" & i & "_AvgWaitTime")) Then
                Dim dValue As Single = moTrendData.Rows(0).Item("P" & i & "_AvgWaitTime")
                oAvgWaitTimeValues(i - 1) = dValue

                If sSDMin_2 = Single.IsNaN(sSDMin_2) Then
                    sSDMin_2 = dValue

                ElseIf dValue < sSDMin_1 Then
                    sSDMin_2 = dValue
                End If

                If Single.IsNaN(sSDMax_2) Then
                    sSDMax_2 = dValue

                ElseIf dValue > sSDMax_1 Then
                    sSDMax_2 = dValue
                End If

                dSD_Count_2 = dSD_Count_1 + 1
                sSDSumSQR_2 = sSDSumSQR_1 + dValue * dValue
                sSDSum_2 = sSDSum_1 + dValue
            End If

            If IsNumeric(moTrendData.Rows(0).Item("P" & i & "_AvgSignalTime")) Then
                Dim dValue As Single = moTrendData.Rows(0).Item("P" & i & "_AvgSignalTime")
                oAvgSignalTimeValues(i - 1) = dValue

                If Single.IsNaN(sSDMin_3) Then
                    sSDMin_3 = dValue

                ElseIf dValue < sSDMin_1 Then
                    sSDMin_3 = dValue
                End If

                If Single.IsNaN(sSDMax_3) Then
                    sSDMax_3 = dValue

                ElseIf dValue > sSDMax_1 Then
                    sSDMax_3 = dValue
                End If

                dSD_Count_3 = dSD_Count_1 + 1
                sSDSumSQR_3 = sSDSumSQR_1 + dValue * dValue
                sSDSum_3 = sSDSum_1 + dValue
            End If
        Next

        '---Calculate average
        sSDAverage_1 = sSDSum_1 / dSD_Count_1
        sSDAverage_2 = sSDSum_2 / dSD_Count_2
        sSDAverage_3 = sSDSum_3 / dSD_Count_3

        '---Now that we know the sum of X2
        '     we can calculate the interim value for standard deviation.
        For i As Integer = 1 To lCount
            If IsNumeric(moTrendData.Rows(0).Item("P" & i & "_AvgRequests")) Then
                Dim dValue As Single = moTrendData.Rows(0).Item("P" & i & "_AvgRequests")
                sSDInterim_1 = sSDInterim_1 + ((dValue - sSDAverage_1) ^ 2) / dSD_Count_1
            End If

            If IsNumeric(moTrendData.Rows(0).Item("P" & i & "_AvgWaitTime")) Then
                Dim dValue As Single = moTrendData.Rows(0).Item("P" & i & "_AvgWaitTime")
                sSDInterim_2 = sSDInterim_2 + ((dValue - sSDAverage_2) ^ 2) / dSD_Count_2
            End If

            If IsNumeric(moTrendData.Rows(0).Item("P" & i & "_AvgSignalTime")) Then
                Dim dValue As Single = moTrendData.Rows(0).Item("P" & i & "_AvgSignalTime")
                sSDInterim_3 = sSDInterim_3 + ((dValue - sSDAverage_3) ^ 2) / dSD_Count_3
            End If
        Next i

        '---Calculate standard deviation
        sSDStdDev_1 = Math.Sqrt(sSDInterim_1)
        sSDStdDev_2 = Math.Sqrt(sSDInterim_2)
        sSDStdDev_3 = Math.Sqrt(sSDInterim_3)

        '---Calculate limits
        Dim sSDLimit_1 As Single = sSDAverage_1 + sSDStdDev_1 * 3
        Dim sSDLimit_2 As Single = sSDAverage_2 + sSDStdDev_2 * 3
        Dim sSDLimit_3 As Single = sSDAverage_3 + sSDStdDev_3 * 3

        '---Determine max
        lMax = sSDLimit_1
        If sSDLimit_2 > lMax Then lMax = sSDLimit_2
        If sSDLimit_3 > lMax Then lMax = sSDLimit_3

        '---We'll create an XAxis object using the ChartDimensions object
        '   from the chart we just created.
        Dim oXAxis As New jChart.XAxis(moChart.ChartDimensions, oXAxisValues)

        '---Let's tell the X Axis about the friendly labels to display in place
        '   of numbers.
        oXAxis.XAxisLabels = oXAxisLabels
        oXAxis.FontAngle = jChart.XAxis.Rotation.Diagonal

        '---OK, finally let's tell the chart about its X Axis.
        moChart.XAxis = oXAxis

        '---Now, let's set up the Y Axis.
        Dim oYAxis1 As New jChart.YAxis(jChart.YAxis.Alignment.Left, moChart.ChartDimensions, 5, 0, lMax)

        Dim oYLabels(5) As String
        For i As Integer = 0 To 5
            oYLabels(i) = (lMax / 5) * i
        Next

        oYAxis1.YAxisLabels = oYLabels

        '---Now let's tell the chart about our Y Axis 
        moChart.YAxis1 = oYAxis1

        '---Now that we have values, we'll need a series
        '   Our series will be a bar series.

        Dim oLineSeries1 As New jChart.Overlay.Series.LineSeries
        With oLineSeries1
            '---We will call our series "series one". 
            '   This value is accessible on drill-down from the image map.
            .Name = "Average Requests"

            '---Now we'll give the series the values it should use.
            .Values = oAvgRequestValues

            '---The series will be green in color
            .Color = Color.Aqua

            '---The series will be slightly transparent (75% opaque)
            .Opacity = 0.75

            .NullValue = NODATA
        End With

        Dim oLineSeries2 As New jChart.Overlay.Series.LineSeries
        With oLineSeries2
            '---We will call our series "series one". 
            '   This value is accessible on drill-down from the image map.
            .Name = "Average Wait Time"

            '---Now we'll give the series the values it should use.
            .Values = oAvgWaitTimeValues

            '---The series will be green in color
            .Color = Color.OrangeRed

            '---The series will be slightly transparent (75% opaque)
            .Opacity = 0.75

            .NullValue = NODATA
        End With

        Dim oLineSeries3 As New jChart.Overlay.Series.LineSeries
        With oLineSeries3
            '---We will call our series "series one". 
            '   This value is accessible on drill-down from the image map.
            .Name = "Average Signal Time"

            '---Now we'll give the series the values it should use.
            .Values = oAvgSignalTimeValues

            '---The series will be green in color
            .Color = Color.Firebrick

            '---The series will be slightly transparent (75% opaque)
            .Opacity = 0.75

            .NullValue = NODATA
        End With

        '---Since we are using a line chart, which belongs to the 
        '   LineOverlay overlay type, we'll need a LineOverlay object.
        Dim oLineOverlay As jChart.Overlay.LineOverlay

        '---We need to tell the overlay which dimensions and axes to use when
        '   we create it.
        oLineOverlay = New jChart.Overlay.LineOverlay(moChart.ChartDimensions, oXAxis, oYAxis1)

        '---Let's add the series to the overlay
        oLineOverlay.Series.Add(oLineSeries1)
        oLineOverlay.Series.Add(oLineSeries2)
        oLineOverlay.Series.Add(oLineSeries3)

        '---Let's add the overlay to the chart.
        moChart.OverLays.Add(oLineOverlay)

        '---OK. All done. Now let's draw the chart.
        '   We'll start by creating a bitmap onto which the chart will be drawn.
        '   We do it this way so that the bitmap can be shared (a background image
        '   perhaps?)
        Dim oPicture As New Bitmap( _
                moChart.ChartDimensions.OuterWidth, _
                moChart.ChartDimensions.OuterHeight, _
                System.Drawing.Imaging.PixelFormat.Format24bppRgb)

        '---Now that we've created the bitmap, we'll draw the chart on it and save it
        '   to a temporary location on the server in JPEG format. The charting object
        '   creates a unique filename for our new chart. This unique filename is
        '   accessible via the chart's FileName property (see ImageURL)

        '---The path where the image will be saved must have write access for the ASPNET
        '   process enabled.
        moChart.SaveChartToFile( _
                    Application("TempPath"), _
                    System.Drawing.Imaging.ImageFormat.Jpeg)
    End Sub

    Private Sub MakeWaitStateDetailChart()
        '---Let's set up the chart.
        '   We'll make it 600 pixels wide by 400 pixels high.        
        moChart = New jChart.GraphChart(510, 260)
        moChart.ChartName = "Average WaitStates - " & msWaitState
        moChart.ChartDimensions.BackgroundColor = Color.White

        '---We'll specify a 10 pixel border all the way around the inside
        '   edge of the chart.
        moChart.ChartDimensions.FrameSize = 10

        '---The Y Axis column on the left will be 20 pixels wide.
        moChart.ChartDimensions.YAxis1LabelColumnWidth = 20

        '---The X Axis row on the bottom will be 20 pixels high.
        moChart.ChartDimensions.XAxisLabelRowHeight = 20

        '---Let's add the X Axis values
        '   We'll have 7 values and in place of the numeric values we'll
        '   display friendly labels.
        Dim lCount As Integer = moTrendData.Rows.Count
        Dim lMax As Long = 0

        Dim oXAxisValues(lCount) As Single
        Dim oXAxisLabels(lCount) As String
        Dim oAvgRequestValues(lCount) As Single
        Dim oAvgWaitTimeValues(lCount) As Single
        Dim oAvgSignalTimeValues(lCount) As Single

        Dim dSD_Count_1 As Integer = 0
        Dim sSDSumSQR_1 As Single = 0
        Dim sSDSum_1 As Single = 0
        Dim sSDAverage_1 As Single = 0
        Dim sSDInterim_1 As Single = 0
        Dim sSDMin_1 As Single = Single.NaN
        Dim sSDMax_1 As Single = Single.NaN
        Dim sSDStdDev_1 As Single

        Dim dSD_Count_2 As Integer = 0
        Dim sSDSumSQR_2 As Single = 0
        Dim sSDSum_2 As Single = 0
        Dim sSDAverage_2 As Single = 0
        Dim sSDInterim_2 As Single = 0
        Dim sSDMin_2 As Single = Single.NaN
        Dim sSDMax_2 As Single = Single.NaN
        Dim sSDStdDev_2 As Single

        Dim dSD_Count_3 As Integer = 0
        Dim sSDSumSQR_3 As Single = 0
        Dim sSDSum_3 As Single = 0
        Dim sSDAverage_3 As Single = 0
        Dim sSDInterim_3 As Single = 0
        Dim sSDMin_3 As Single = Single.NaN
        Dim sSDMax_3 As Single = Single.NaN
        Dim sSDStdDev_3 As Single

        Dim iLabelInterval As Integer = lCount / 10

        oXAxisLabels(0) = CDate(msStartDate).ToString("ddd/HH:mm")

        For i As Integer = 1 To lCount
            oXAxisValues(i - 1) = i - 1

            If i Mod iLabelInterval = 0 Then
                oXAxisLabels(i - 1) = DateAdd(DateInterval.Hour, (i - 1), CDate(msStartDate)).ToString("ddd/HH:mm")
            End If

            If IsNumeric(moTrendData.Rows(i - 1).Item("dAverageRequests")) Then
                Dim dValue As Single = moTrendData.Rows(i - 1).Item("dAverageRequests")
                oAvgRequestValues(i - 1) = dValue

                If Single.IsNaN(sSDMin_1) Then
                    sSDMin_1 = dValue

                ElseIf dValue < sSDMin_1 Then
                    sSDMin_1 = dValue
                End If

                If Single.IsNaN(sSDMax_1) Then
                    sSDMax_1 = dValue

                ElseIf dValue > sSDMax_1 Then
                    sSDMax_1 = dValue
                End If

                dSD_Count_1 = dSD_Count_1 + 1
                sSDSumSQR_1 = sSDSumSQR_1 + dValue * dValue
                sSDSum_1 = sSDSum_1 + dValue
            Else
                oAvgRequestValues(i - 1) = NODATA
            End If

            If IsNumeric(moTrendData.Rows(i - 1).Item("dAverageWait_Time")) Then
                Dim dValue As Single = moTrendData.Rows(i - 1).Item("dAverageWait_Time")
                oAvgWaitTimeValues(i - 1) = dValue

                If Single.IsNaN(sSDMin_2) Then
                    sSDMin_2 = dValue

                ElseIf dValue < sSDMin_2 Then
                    sSDMin_2 = dValue
                End If

                If Single.IsNaN(sSDMax_2) Then
                    sSDMax_2 = dValue

                ElseIf dValue > sSDMax_2 Then
                    sSDMax_2 = dValue
                End If

                dSD_Count_2 = dSD_Count_2 + 1
                sSDSumSQR_2 = sSDSumSQR_2 + dValue * dValue
                sSDSum_2 = sSDSum_2 + dValue
            Else
                oAvgWaitTimeValues(i - 1) = NODATA
            End If

            If IsNumeric(moTrendData.Rows(i - 1).Item("dAverageSignal_Wait_Time")) Then
                Dim dValue As Single = moTrendData.Rows(i - 1).Item("dAverageSignal_Wait_Time")
                oAvgSignalTimeValues(i - 1) = dValue

                If Single.IsNaN(sSDMin_3) Then
                    sSDMin_3 = dValue

                ElseIf dValue < sSDMin_3 Then
                    sSDMin_3 = dValue
                End If

                If Single.IsNaN(sSDMax_3) Then
                    sSDMax_1 = dValue

                ElseIf dValue > sSDMax_3 Then
                    sSDMax_3 = dValue
                End If

                dSD_Count_3 = dSD_Count_3 + 1
                sSDSumSQR_3 = sSDSumSQR_3 + dValue * dValue
                sSDSum_3 = sSDSum_3 + dValue
            Else
                oAvgSignalTimeValues(i - 1) = NODATA
            End If
        Next

        '---Calculate average
        sSDAverage_1 = sSDSum_1 / dSD_Count_1
        sSDAverage_2 = sSDSum_2 / dSD_Count_2
        sSDAverage_3 = sSDSum_3 / dSD_Count_3

        '---Now that we know the sum of X2
        '     we can calculate the interim value for standard deviation.
        For i As Integer = 1 To lCount
            If IsNumeric(moTrendData.Rows(i - 1).Item("dAverageRequests")) Then
                Dim dValue As Single = moTrendData.Rows(i - 1).Item("dAverageRequests")
                sSDInterim_1 = sSDInterim_1 + ((dValue - sSDAverage_1) ^ 2) / dSD_Count_1
            End If

            If IsNumeric(moTrendData.Rows(i - 1).Item("dAverageWait_Time")) Then
                Dim dValue As Single = moTrendData.Rows(i - 1).Item("dAverageWait_Time")
                sSDInterim_2 = sSDInterim_2 + ((dValue - sSDAverage_2) ^ 2) / dSD_Count_2
            End If

            If IsNumeric(moTrendData.Rows(i - 1).Item("dAverageSignal_Wait_Time")) Then
                Dim dValue As Single = moTrendData.Rows(i - 1).Item("dAverageSignal_Wait_Time")
                sSDInterim_3 = sSDInterim_3 + ((dValue - sSDAverage_3) ^ 2) / dSD_Count_3
            End If
        Next i

        '---Calculate standard deviation
        sSDStdDev_1 = Math.Sqrt(sSDInterim_1)
        sSDStdDev_2 = Math.Sqrt(sSDInterim_2)
        sSDStdDev_3 = Math.Sqrt(sSDInterim_3)

        '---Calculate limits
        Dim sSDLimit_1 As Single = sSDAverage_1 + sSDStdDev_1 * 3
        Dim sSDLimit_2 As Single = sSDAverage_2 + sSDStdDev_2 * 3
        Dim sSDLimit_3 As Single = sSDAverage_3 + sSDStdDev_3 * 3

        '---Determine max
        lMax = sSDLimit_1
        If sSDLimit_2 > 0 AndAlso sSDLimit_2 < lMax Then lMax = sSDLimit_2
        If sSDLimit_3 > 0 AndAlso sSDLimit_3 < lMax Then lMax = sSDLimit_3

        oXAxisLabels(lCount - 1) = CDate(msEndDate).ToString("ddd/HH:mm")

        '---We'll create an XAxis object using the ChartDimensions object
        '   from the chart we just created.
        Dim oXAxis As New jChart.XAxis(moChart.ChartDimensions, oXAxisValues)

        '---Let's tell the X Axis about the friendly labels to display in place
        '   of numbers.
        oXAxis.XAxisLabels = oXAxisLabels
        oXAxis.FontAngle = jChart.XAxis.Rotation.Diagonal
        oXAxis.ShowLines = False

        '---OK, finally let's tell the chart about its X Axis.
        moChart.XAxis = oXAxis

        '---Now, let's set up the Y Axis.
        Dim oYAxis1 As New jChart.YAxis(jChart.YAxis.Alignment.Left, moChart.ChartDimensions, 5, 0, lMax)

        Dim oYLabels(5) As String
        For i As Integer = 0 To 5
            oYLabels(i) = (lMax / 5) * i
        Next

        oYAxis1.YAxisLabels = oYLabels
        oYAxis1.ShowLines = False

        '---Now let's tell the chart about our Y Axis 
        moChart.YAxis1 = oYAxis1

        '---Now that we have values, we'll need a series
        '   Our series will be a bar series.

        Dim oLineSeries1 As New jChart.Overlay.Series.LineSeries
        With oLineSeries1
            '---We will call our series "series one". 
            '   This value is accessible on drill-down from the image map.
            .Name = "Average Requests"

            '---Now we'll give the series the values it should use.
            .Values = oAvgRequestValues

            '---The series will be green in color
            .Color = Color.Aqua

            '---The series will be slightly transparent (75% opaque)
            .Opacity = 0.75

            .NullValue = NODATA
        End With

        Dim oLineSeries2 As New jChart.Overlay.Series.LineSeries
        With oLineSeries2
            '---We will call our series "series one". 
            '   This value is accessible on drill-down from the image map.
            .Name = "Average Wait Time"

            '---Now we'll give the series the values it should use.
            .Values = oAvgWaitTimeValues

            '---The series will be green in color
            .Color = Color.OrangeRed

            '---The series will be slightly transparent (75% opaque)
            .Opacity = 0.75

            .NullValue = NODATA
        End With

        Dim oLineSeries3 As New jChart.Overlay.Series.LineSeries
        With oLineSeries3
            '---We will call our series "series one". 
            '   This value is accessible on drill-down from the image map.
            .Name = "Average Signal Time"

            '---Now we'll give the series the values it should use.
            .Values = oAvgSignalTimeValues

            '---The series will be green in color
            .Color = Color.Firebrick

            '---The series will be slightly transparent (75% opaque)
            .Opacity = 0.75

            .NullValue = NODATA
        End With

        '---Since we are using a line chart, which belongs to the 
        '   LineOverlay overlay type, we'll need a LineOverlay object.
        Dim oLineOverlay As jChart.Overlay.LineOverlay

        '---We need to tell the overlay which dimensions and axes to use when
        '   we create it.
        oLineOverlay = New jChart.Overlay.LineOverlay(moChart.ChartDimensions, oXAxis, oYAxis1)

        '---Let's add the series to the overlay
        oLineOverlay.Series.Add(oLineSeries1)
        oLineOverlay.Series.Add(oLineSeries2)
        oLineOverlay.Series.Add(oLineSeries3)

        '---Let's add the overlay to the chart.
        moChart.OverLays.Add(oLineOverlay)

        '---OK. All done. Now let's draw the chart.
        '   We'll start by creating a bitmap onto which the chart will be drawn.
        '   We do it this way so that the bitmap can be shared (a background image
        '   perhaps?)
        Dim oPicture As New Bitmap( _
                moChart.ChartDimensions.OuterWidth, _
                moChart.ChartDimensions.OuterHeight, _
                System.Drawing.Imaging.PixelFormat.Format24bppRgb)

        '---Now that we've created the bitmap, we'll draw the chart on it and save it
        '   to a temporary location on the server in JPEG format. The charting object
        '   creates a unique filename for our new chart. This unique filename is
        '   accessible via the chart's FileName property (see ImageURL)

        '---The path where the image will be saved must have write access for the ASPNET
        '   process enabled.
        moChart.SaveChartToFile( _
                    Application("TempPath"), _
                    System.Drawing.Imaging.ImageFormat.Gif)
    End Sub

    Protected Sub ShowImageTag()
        If Not moChart Is Nothing Then
            Dim sHTM As String = "<img src=" & Q & Session("TempURL") & moChart.FileName & Q & " alt=" & Q & Q & " usemap=" & Q & "#MyMap" & Q & " />"

            sHTM = sHTM & "<map id=" & Q & "Map1" & Q & " name=" & Q & "MyMap" & Q & ">"

            If mbShowDetail = False Then
                sHTM = sHTM & MakeSummaryImageMapString()
            End If

            sHTM = sHTM & "</map>"

            Response.Write(sHTM)
        End If
    End Sub

    Private Function MakeSummaryImageMapString() As String
        Dim sImageMap As String = ""
        Dim sStartDate As String = ""
        Dim sEndDate As String = ""

        '---The GetAllRegions function of the Chart object gets all regions
        '   of all series of all overlays, in order of display.
        Dim oRow As DataRow = moTrendData.Rows(0)

        For Each oRegion As jChart.RegionArea In moChart.RegionList
            Dim sCoords As String = ""

            '---The SeriesPoints property of the RegionArea object gives
            '   us the coordinates we need to describe a polygon around each
            '   series data point.
            Dim oPoints As Point() = oRegion.SeriesPoints
            sCoords = oPoints(0).X & "," & oPoints(0).Y

            For iPoint As Integer = 1 To oPoints.GetUpperBound(0)
                sCoords = sCoords & "," & oPoints(iPoint).X & "," & oPoints(iPoint).Y
            Next

            '---Now that we have the polygon, let's tell the web browser what 
            '   to do when our user clicks on a data point. Here we'll just display
            '   a javascript message, but anything is possible...
            Dim sArea As String = "<area " & _
                                        " shape=" & Chr(34) & "poly" & Chr(34) & _
                                        " coords=" & Chr(34) & sCoords & Chr(34)

            Dim lXValue As Long = oRegion.XValue
            Dim lYValue As Long = oRegion.YValue

            sStartDate = DateAdd(DateInterval.Day, (lXValue) * mdIntervals, CDate(msStartDate)).Date.ToString("MM/dd/yyyy")
            If lXValue < 9 Then
                sEndDate = DateAdd(DateInterval.Day, (lXValue + 1) * mdIntervals, CDate(msStartDate)).Date.ToString("MM/dd/yyyy")
            Else
                sEndDate = ""
            End If

            sArea = sArea & " href=" & Chr(34) & "SQWaitStatesL.aspx?D=" & msDBID & "&SD=" & HttpUtility.UrlEncode(sStartDate) & "&ED=" & HttpUtility.UrlEncode(sEndDate) & "&T=" & msWaitState & Q

            sArea = sArea & " alt=" & Q & oRegion.SeriesName & Q & _
                          " value=" & Q & oRegion.YValue & Q & _
                          " title=" & Q & oRegion.SeriesName & " for " & sStartDate & Q & " is " & lYValue & ">" & _
                                        vbCrLf


            sImageMap = sImageMap & sArea
        Next

        Return sImageMap
    End Function

    Protected Sub ShowTable()
        If mbShowDetail = False Then
            ShowSummaryTable()
        Else
            ShowDetailTable()
        End If
    End Sub

    Private Sub ShowSummaryTable()
        Dim oHTM As New StringBuilder("<table style=" & Q & "width: 515px; border-collapse: collapse; border: solid 1px grey;" & Q & ">")

        oHTM.Append("<tr style=" & Q & "height: 20px; vertical-align: bottom;" & Q & ">")
        oHTM.Append("<td style=" & Q & "width: 135px; border-bottom: solid 1px grey;" & Q & ">&nbsp;</td>")
        oHTM.Append("<td style=" & Q & "width: 125px; border-bottom: solid 1px grey;" & Q & ">Avg Requests</td>")
        oHTM.Append("<td style=" & Q & "width: 125px; border-bottom: solid 1px grey;" & Q & ">Avg Wait Time</td>")
        oHTM.Append("<td style=" & Q & "width: 125px; border-bottom: solid 1px grey;" & Q & ">Avg Signal Time</td>")
        oHTM.Append("</tr>")

        Dim bAlternatingColor As Boolean = False
        Dim sStartDate As String
        Dim sEndDate As String

        For i As Integer = 1 To 10
            sStartDate = DateAdd(DateInterval.Day, (i - 1) * mdIntervals, CDate(msStartDate)).Date.ToString("MM/dd/yyyy")
            If i < 10 Then
                sEndDate = DateAdd(DateInterval.Day, i * mdIntervals, CDate(msStartDate)).Date.ToString("MM/dd/yyyy")
            Else
                sEndDate = "Present"
            End If

            If bAlternatingColor = False Then
                oHTM.Append("<tr class=" & Q & "wmObjectList_T" & Q & " style=" & Q & "height:25px;" & Q & ">")
            Else
                oHTM.Append("<tr class=" & Q & "wmObjectList_TA" & Q & " style=" & Q & "height:25px;" & Q & ">")
            End If

            oHTM.Append("<td>" & sStartDate & _
                                 "<br/> to " & _
                                 sEndDate & _
                         "</td>")

            oHTM.Append("<td>")
            If Not moTrendData.Rows(0).Item("P" & i & "_AvgRequests") Is System.DBNull.Value _
                    AndAlso moTrendData.Rows(0).Item("P" & i & "_AvgRequests") > 0 Then

                oHTM.Append("<a href=" & Q & "SQWaitStatesL.aspx?D=" & msDBID & "&SD=" & HttpUtility.UrlEncode(sStartDate) & "&ED=" & HttpUtility.UrlEncode(sEndDate) & "&T=" & msWaitState & Q & ">")
                oHTM.Append(moTrendData.Rows(0).Item("P" & i & "_AvgRequests"))
                oHTM.Append("</a>")
            Else
                oHTM.Append("&nbsp;")
            End If
            oHTM.Append("</td>")

            oHTM.Append("<td>")
            If Not moTrendData.Rows(0).Item("P" & i & "_AvgRequests") Is System.DBNull.Value _
                AndAlso moTrendData.Rows(0).Item("P" & i & "_AvgWaitTime") > 0 Then

                oHTM.Append("<a href=" & Q & "SQWaitStatesL.aspx?D=" & msDBID & "&SD=" & HttpUtility.UrlEncode(sStartDate) & "&ED=" & HttpUtility.UrlEncode(sEndDate) & "&T=" & msWaitState & Q & ">")
                oHTM.Append(moTrendData.Rows(0).Item("P" & i & "_AvgWaitTime"))
                oHTM.Append("</a>")
            Else
                oHTM.Append("&nbsp;")
            End If
            oHTM.Append("</td>")

            oHTM.Append("<td>")
            If Not moTrendData.Rows(0).Item("P" & i & "_AvgRequests") Is System.DBNull.Value _
                AndAlso moTrendData.Rows(0).Item("P" & i & "_AvgSignalTime") > 0 Then

                oHTM.Append("<a href=" & Q & "SQWaitStatesL.aspx?D=" & msDBID & "&SD=" & HttpUtility.UrlEncode(sStartDate) & "&ED=" & HttpUtility.UrlEncode(sEndDate) & "&T=" & msWaitState & Q & ">")
                oHTM.Append(moTrendData.Rows(0).Item("P" & i & "_AvgSignalTime"))
                oHTM.Append("</a>")
            Else
                oHTM.Append("&nbsp;")
            End If
            oHTM.Append("</td>")

            oHTM.Append("</tr>")

            bAlternatingColor = Not bAlternatingColor
        Next

        oHTM.Append("</table>")

        Response.Write(oHTM.ToString)
    End Sub

    Private Sub ShowDetailTable()
        Dim oHTM As New StringBuilder("<table style=" & Q & "width: 515px; border-collapse: collapse; border: solid 1px grey;" & Q & ">")

        oHTM.Append("<tr style=" & Q & "height: 20px; vertical-align: bottom;" & Q & ">")
        oHTM.Append("<td style=" & Q & "width: 135px; border-bottom: solid 1px grey;" & Q & ">&nbsp;</td>")
        oHTM.Append("<td style=" & Q & "width: 125px; border-bottom: solid 1px grey;" & Q & ">Avg Requests</td>")
        oHTM.Append("<td style=" & Q & "width: 125px; border-bottom: solid 1px grey;" & Q & ">Avg Wait Time</td>")
        oHTM.Append("<td style=" & Q & "width: 125px; border-bottom: solid 1px grey;" & Q & ">Avg Signal Time</td>")
        oHTM.Append("</tr>")

        Dim bAlternatingColor As Boolean = False
        Dim sDate As String

        For Each oRow As DataRow In moTrendData.Rows
            sDate = CDate(oRow.Item("dtDate")).ToString("ddd/HH:mm")

            If bAlternatingColor = False Then
                oHTM.Append("<tr class=" & Q & "wmObjectList_T" & Q & " style=" & Q & "height:25px;" & Q & ">")
            Else
                oHTM.Append("<tr class=" & Q & "wmObjectList_TA" & Q & " style=" & Q & "height:25px;" & Q & ">")
            End If

            oHTM.Append("<td>" & sDate & "</td>")

            oHTM.Append("<td>")
            If Not oRow.Item("dAverageRequests") Is System.DBNull.Value _
                    AndAlso oRow.Item("dAverageRequests") > 0 Then

                oHTM.Append(oRow.Item("dAverageRequests"))
            Else
                oHTM.Append("&nbsp;")
            End If
            oHTM.Append("</td>")

            oHTM.Append("<td>")
            If Not oRow.Item("dAverageWait_Time") Is System.DBNull.Value _
                    AndAlso oRow.Item("dAverageWait_Time") > 0 Then

                oHTM.Append(oRow.Item("dAverageWait_Time"))
            Else
                oHTM.Append("&nbsp;")
            End If
            oHTM.Append("</td>")

            oHTM.Append("<td>")
            If Not oRow.Item("dAverageSignal_Wait_Time") Is System.DBNull.Value _
                    AndAlso oRow.Item("dAverageSignal_Wait_Time") > 0 Then

                oHTM.Append(oRow.Item("dAverageSignal_Wait_Time"))
            Else
                oHTM.Append("&nbsp;")
            End If
            oHTM.Append("</td>")

            oHTM.Append("</tr>")

            bAlternatingColor = Not bAlternatingColor
        Next

        oHTM.Append("</table>")

        Response.Write(oHTM.ToString)
    End Sub

    Private Sub RegisterClientScript( _
                        ByVal sScriptKey As String, _
                        ByVal sJSFunction As String)

        If Not ClientScript.IsClientScriptBlockRegistered(sScriptKey) Then
            Dim sScript As New StringBuilder("<script language=" & Q & "javascript" & Q & " type=" & Q & "text/javascript" & Q & ">")

            sScript.Append(sJSFunction)

            sScript.Append("</script>")

            ClientScript.RegisterStartupScript(GetType(Page), sScriptKey, sScript.ToString)
        End If
    End Sub
End Class
